package cn.kys.modules.system.controller;

import cn.kys.common.annotation.EnableKysAnonymousAccess;
import cn.kys.modules.system.service.GeoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**功能描述：点选、框选查询
 * @Auther:wyq
 * @Date:2020/9/21
 * @Description:cn.kys.modules.system.controller
 * @version:1.0
 */
@Api(tags = "系统：信息查询")
@RestController
@RequestMapping("/api/geo")
public class GeoController {

    private final UserDetailsService userDetailsService;
    private final GeoService geoService;

    public GeoController(UserDetailsService userDetailsService, GeoService geoService) {
        this.userDetailsService = userDetailsService;
        this.geoService = geoService;
    }

    @ApiOperation("点击查询")
    @GetMapping("/findGeo/{lat}/{lon:.+}")
    @EnableKysAnonymousAccess
    public ResponseEntity<Object> findGeo(@PathVariable("lat") String lat, @PathVariable("lon") String lon){

        List returnList = new ArrayList();
        int count_building = geoService.findCount(lat, lon,"t_building");
        int count_greenland = geoService.findCount(lat, lon,"t_greenland");

        /*if(count_building>0){
            Map map = new HashMap();
            List list = geoService.findGeo(lat, lon,"t_building");
            map.put("layer", "building");
            map.put("data", list);
            returnList.add(map);
        }

        if(count_greenland>0){
            Map map = new HashMap();
            List list = geoService.findGeo(lat, lon,"t_greenland");
            map.put("layer", "greenland");
            map.put("data", list);
            returnList.add(map);
        }*/
        List listBuilding = null;
        if(count_building>0){

            listBuilding = geoService.findGeo(lat, lon,"t_building");
            for(int i=0;i<listBuilding.size();i++){
                ((Map)listBuilding.get(i)).put("layer", "建筑物");
                returnList.add(listBuilding.get(i));
            }

        }
        List listGreenland = null;
        if(count_greenland>0){

            listGreenland = geoService.findGeo(lat, lon,"t_greenland");
            for(int i=0;i<listGreenland.size();i++){
                ((Map)listGreenland.get(i)).put("layer", "绿地");
                returnList.add(listGreenland.get(i));
            }
        }

        return ResponseEntity.ok(returnList);
    }

    @ApiOperation("框选查询")
    @GetMapping("/findGeoByIntersection/{polygon}/{types}")
    @EnableKysAnonymousAccess
    public ResponseEntity<Object> findGeoByIntersection(@PathVariable("polygon") String polygon, @PathVariable("types") String types){


        Map returnMap = new HashMap();
        try {

            List list = geoService.findGeoPolygon(polygon,types);

            returnMap.put("data",list);
            returnMap.put("success",true);
            returnMap.put("resMsg","获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success",false);
            returnMap.put("resMsg","异常");
        }

        return ResponseEntity.ok(returnMap);
    }
}
